import { menu } from '../../../app/decorator';
import BaseShap from '../shap/base/BaseShap';
const { ccclass } = cc._decorator;

@ccclass
@menu(__filename, '形状触摸')
export default class TouchPolygon extends cc.Component {
    private _hitTest(point: cc.Vec2, listener) {
        const result = cc.Node.prototype['_hitTest'].call(this.node, point, listener);
        const shaps = this.getComponents(BaseShap);

        if (shaps && shaps.length) {
            point = this.node.convertToNodeSpaceAR(point);
            return result && shaps.some(function (shap) {
                return shap.contains(point);
            });
        } else {
            if (CC_DEBUG) cc.warn(`${this.node.name}: 当前未绑定shap组件`);
            return result;
        }
    }

    onEnable() {
        this.node['_hitTest'] = this._hitTest.bind(this);
    }

    onDisable() {
        delete this.node['_hitTest'];
    }
}